//
//  MainTabbarController.m
//  YuErmuYing
//
//  Created by Tiancheng on 16/8/8.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "MainTabbarController.h"

#import "homeViewController.h"
#import "communityViewController.h"
//#import "orderViewController.h"
#import "mineViewController.h"

#define iOS7 ([[UIDevice currentDevice].systemVersion doubleValue] >= 7.0)

@interface MainTabbarController ()

@end

@implementation MainTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
static MainTabbarController *singleton = nil;

+(id)sharedMainTabbar;
{
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        singleton = [[self alloc] init];
        if (iOS7) {
            singleton.edgesForExtendedLayout = UIRectEdgeNone;
        }
        [singleton addAllChildViewControllers];
        //设置地下分栏的颜色
        singleton.tabBar.barTintColor = [UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
    });
    return singleton;
}
- (void)addAllChildViewControllers
{
//    NSArray *buttonImages = @[@"tabBar_first",@"tabBar_help", @"tabBar_map", @"tabBar_mine"];
//    NSArray *buttonImages = @[@"tabBar_first",@"tabBar_help",  @"tabBar_mine"];
    //未选中的颜色
        NSArray *buttonImages = @[@"tabBar_firstHL",@"tabBar_helpHL", @"tabBar_mineHL"];


//    NSArray *buttonImagesSelected = @[@"tabBar_firstHL",@"tabBar_helpHL", @"tabBar_mapHL", @"tabBar_mineHL"];
//    NSArray *buttonImagesSelected = @[@"tabBar_firstHL",@"tabBar_helpHL", @"tabBar_mineHL"];
    //选中的颜色
    NSArray *buttonImagesSelected = @[@"tabBar_first",@"tabBar_help",  @"tabBar_mine"];


    homeViewController *appointVC=[homeViewController new];
    [self addChildViewController:appointVC title:@"首页" image:buttonImages[0] selectedImage:buttonImagesSelected[0]];
    communityViewController*finderVC=[communityViewController new];
    [self addChildViewController:finderVC title:@"订单" image:buttonImages[1] selectedImage:buttonImagesSelected[1]];
//    orderViewController *orderVC=[orderViewController new];
//    [self addChildViewController:orderVC title:@"订购" image:buttonImages[2] selectedImage:buttonImagesSelected[2]];
    mineViewController *mineVC=[mineViewController new];
    [self addChildViewController:mineVC title:@"我的" image:buttonImages[2] selectedImage:buttonImagesSelected[2]];
}
#pragma mark -添加一个控制器
/**
 *  添加一个子控制器
 *
 *  @param childController 控制器
 *  @param title           title®
 *  @param image           image
 *  @param selectedImage   selectedImage
 */
-(void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    //1.tababr标题
    childController.tabBarItem.title=title;
    UIImage *normal=[UIImage imageNamed:image];
    UIImage *selected=[UIImage imageNamed:selectedImage];
    //未选中的颜色
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    //选中的颜色
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:136/255.0 green:14/255.0 blue:79/255.0 alpha:1], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    
    if (iOS7) {
        childController.tabBarItem.image = [normal imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        childController.tabBarItem.selectedImage = [selected imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }else{
        childController.tabBarItem.selectedImage = selected;
    }
    
    //2.包装导航控制器
    childController.title=title;
    UINavigationController *unc = [[UINavigationController alloc] initWithRootViewController:childController];
    [self addChildViewController:unc];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
